package main

import "fmt"

func main() {
	println(convert("LEETCODEISHIRING", 3))
	println(convert2("LEETCODEISHIRING", 3))
}

func convert(s string, numRows int) string {

	if numRows == 1 {
		return s
	}

	maps := make(map[int][]rune)

	row := 1

	down := true
	up := false

	for _, v := range s {
		if row > numRows && down == true {
			down = false
			up = true
			row--
		} else if row < 2 && up == true {
			down = true
			up = false
			row++
		}

		if down {
			maps[row] = append(maps[row], v)
			row++
		}

		if up {
			row--
			maps[row] = append(maps[row], v)
		}

	}

	var resultRune []rune

	for i := 1; i <= numRows; i++ {
		fmt.Println("maps: ", string(maps[i]))
		resultRune = append(resultRune, maps[i]...)
	}

	return string(resultRune)

}


func convert2(s string, numRows int) string {
	if numRows == 1 {
		return s
	}
	// 循环次数为 2 * numRows - 2
	ans := make([][]rune, numRows)
	for i, _ := range ans {
		ans[i] = []rune{}
	}

	for i, letter := range s {
		local := (i + 1) % (2 * numRows - 2)
		if local == 0 {
			local = 2 * numRows - 2
		}
		if local <= numRows {
			ans[local-1] = append(ans[local-1], letter)
		} else {
			ans[(2*numRows-local)-1] = append(ans[(2*numRows-local)-1], letter)
		}
	}
	var result []rune
	for _, an := range ans {
		result = append(result, an...)
	}
	return string(result)
}